package com.liy.framework.common.office.excel;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadExcel {
    public static final String OFFICE_EXCEL_2003_POSTFIX = "xls";
    public static final String OFFICE_EXCEL_2010_POSTFIX = "xlsx";

    public ReadExcel() {
    }

    public static void main(String[] args) {
        try {
            new ReadExcel();
            readExcel("H:\\D达实智能\\AFC_PM\\05.计划\\检测中心平台系统-样品开发计划.xlsx");
        } catch (Exception var2) {
            var2.printStackTrace();
        }

    }

    public static List<Map<Integer, Object>> readExcel(InputStream input, boolean is2010) {
        List<Map<Integer, Object>> listMap = new ArrayList();
        HashMap map = null;

        try {
            Workbook wb = is2010 ? new XSSFWorkbook(input) : new HSSFWorkbook(input);
            Sheet sheet = ((Workbook)wb).getSheetAt(0);
            Iterator rows = sheet.rowIterator();

            while(rows.hasNext()) {
                map = new HashMap();
                Row row = (Row)rows.next();
                Iterator<Cell> cells = row.cellIterator();
                if (row.getRowNum() == 26) {
                    ;
                }

                Object val;
                Cell cell;
                for(; cells.hasNext(); map.put(cell.getColumnIndex(), val)) {
                    val = null;
                    cell = (Cell)cells.next();
                    switch(cell.getCellType()) {
                        case 0:
                            val = cell.getNumericCellValue();
                            break;
                        case 1:
                            val = cell.getStringCellValue();
                            break;
                        case 2:
                            val = cell.getCellFormula();
                            break;
                        case 3:
                        default:
                            val = "";
                            break;
                        case 4:
                            val = cell.getBooleanCellValue();
                    }
                }

                listMap.add(map);
            }
        } catch (IOException var11) {
            var11.printStackTrace();
        }

        return listMap;
    }

    public static List<Map<Integer, Object>> readExcel(String fileName) throws Exception {
        InputStream input = new FileInputStream(fileName);
        return readExcel(input, fileName.endsWith("xlsx"));
    }
}
